<template>
  <button 
    class="back-to-top" 
    :class="{ show: showButton }" 
    :title="title"
    :aria-label="title"
    @click="scrollToTop"
  >
    {{ icon }}
  </button>
</template>

<script>
export default {
  name: 'BackToTop',
  props: {
    showButton: {
      type: Boolean,
      default: false,
    },
    icon: {
      type: String,
      default: '↑',
    },
    title: {
      type: String,
      default: '返回顶部',
    },
    threshold: {
      type: Number,
      default: 300,
    },
    scrollBehavior: {
      type: String,
      default: 'smooth',
    },
  },
  methods: {
    scrollToTop() {
      window.scrollTo({
        top: 0,
        behavior: this.scrollBehavior,
      })
      this.$emit('scrolled-to-top')
    },
  },
}
</script>

<style scoped>
.back-to-top {
  position: fixed;
  bottom: 60px;
  right: 5px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background-color: var(--bilibili-pink, #FB7299);
  color: white;
  border: none;
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  visibility: hidden;
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  z-index: 9999;
}

.back-to-top.show {
  opacity: 1;
  visibility: visible;
}

.back-to-top:hover {
  background-color: #ff5777;
  transform: translateY(-2px);
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
}

.back-to-top:active {
  transform: translateY(0);
}

/* 移动端适配 */
@media (max-width: 768px) {
  .back-to-top {
    bottom: 120px; /* 给底部导航栏留出空间 */
    right: 5px;
    width: 32px;
    height: 32px;
    font-size: 16px;
  }
}
</style>